package com.awesome.uidemo.eventdispatch;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import com.awesome.common.util.log.ShowLogUtil;
import com.awesome.uidemo.R;

/**
 * 长点击
 */
public class LongClickMainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_long_click_main);
//        findViewById(R.id.framelayout).setOnLongClickListener(new View.OnLongClickListener() {
//            @Override
//            public boolean onLongClick(View v) {
//                ShowLogUtil.info("setOnLongClickListener");
//                return false;
//            }
//        });
//        findViewById(R.id.framelayout).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ShowLogUtil.info("setOnClickListener");
//            }
//        });
        final long[] lastTime = {0};
        findViewById(R.id.framelayout).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                ShowLogUtil.info("setOnTouchListener");
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        lastTime[0] = System.currentTimeMillis();
                        ShowLogUtil.info("lastTime = $lastTime");
                        break;
                    case MotionEvent.ACTION_UP:
                        long interval = System.currentTimeMillis() - lastTime[0];
                        ShowLogUtil.info("interval = $interval");
                        if (interval > 1000) {
                            ShowLogUtil.info("setOnLongClickListener");
                        }
                        lastTime[0] = 0;
                        break;
                }
                return true;
            }
        });
    }
}